package com.zijing2333.servicerunner;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSStatuses implements Parcelable {

    private Map<String, JSStatus> jsStatusesMap;

    public JSStatuses(Map<String, JSStatus> jsStatusesMap) {
        this.jsStatusesMap = jsStatusesMap;
    }

    public void setStatus(String taskId, JSStatus status) {
        jsStatusesMap.put(taskId, status);
    }

    public void removeStatus(String taskId) {
        jsStatusesMap.remove(taskId);
    }

    @Override
    public String toString() {
        return "JSStatuses{" +
                "jsStatusesMap=" + jsStatusesMap +
                '}';
    }

    protected JSStatuses(Parcel in) {
        int size = in.readInt();

        List<String> keys = new ArrayList<>();
        in.readStringList(keys);

        List<JSStatus> values = new ArrayList<>();
        in.readTypedList(values, JSStatus.CREATOR);

        jsStatusesMap = new HashMap<>();
        for (int i = 0; i < size; i++) {
            jsStatusesMap.put(keys.get(i), values.get(i));
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        int size = jsStatusesMap.size();

        List<String> keys = new ArrayList<>(jsStatusesMap.keySet());
        List<JSStatus> values = new ArrayList<>(jsStatusesMap.values());
        dest.writeInt(size);
        dest.writeStringList(keys);
        dest.writeTypedList(values);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<JSStatuses> CREATOR = new Creator<JSStatuses>() {
        @Override
        public JSStatuses createFromParcel(Parcel in) {
            return new JSStatuses(in);
        }

        @Override
        public JSStatuses[] newArray(int size) {
            return new JSStatuses[size];
        }
    };

    public Map<String, JSStatus> getJsStatuses() {
        return jsStatusesMap;
    }

    public JSStatus getJsStatusesByTaskId(String taskId){
        return jsStatusesMap.get(taskId);
    }
}
